
// 是否为空
const isEmpty = val => {
    if (val instanceof Array) {
        if (val.length === 0) return true;
    } else if (val instanceof Object) {
        if (!Object.keys(val).length) return true;
    } else {
        if (
            val === "null" ||
            val === null ||
            val === "undefined" ||
            val === undefined ||
            val === ""
        )
            return true;
        return false;
    }
    return false;
};
/**
 * 深拷贝对象
 * @param {要拷贝对象} obj
 */
export const copyObject = obj => {
    let str,
        newobj = Array.isArray(obj) === true ? [] : {};
    if (typeof obj !== "object") {
        return;
    } else if (JSON) {
        (str = JSON.stringify(obj)), //系列化对象
            (newobj = JSON.parse(str)); //还原
    } else {
        for (let i in obj) {
            newobj[i] = typeof obj[i] === "object" ? copyObject(obj[i]) : obj[i];
        }
    }
    return newobj;
};
const numberFormat = (number, type) => {
    if (number > 10000) {
        let num = (number / 10000 + "").split(".");
        return `${num[0]}.${("" + num[1]).slice(0, 1)} 万`;
    } else {
        return number;
    }
};
/**
 * 验证手机号 1开头11位数
 * @parmas phone
 * **/
function verifyPhone(phone){
    let pattern =  /^1\d{10}$/;
    return pattern.test(phone)
};

/**
 * 获取URL参数 code/reference
 */
function getQueryString(name) {
	var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
	var r = window.location.search.substr(1).match(reg);
	if (r != null) {
		return unescape(r[2]);
	}
	return null;
}
/**
 * 截取参数
 */
function clear(str) {
    // str = str.replace(/,/g, "");//取消字符串中出现的所有逗号
    str = str.substr(0,10);//删除第一个字符
    return str;
}
/**
 * 截取手机号  130****852
 */
function hidePhone(phone) {
    phone = phone.substr(0,3)+'***'+phone.substr(phone.length-3,phone.length);//删除第一个字符
    return phone;
}
/**
 * 截取身份证  1300****8520
 */
function hideIdCard(idCard) {
    idCard = idCard.substr(0,4)+'******'+idCard.substr(idCard.length-4,idCard.length);//删除第一个字符
    return idCard;
}
/*
* 数字长度小于1的话在前面加0，用于格式化日期
* */
const formatNumber = (n) => {
    const str = n.toString();
    return str[1] ? str : `0${str}`;
};
/**
* 获取时间
* string date时间，string type时间格式
* */
export const getTime = (type = "yyyy-MM-dd HH:mm:ss",date = new Date().getTime()) => {
    console.log(type)
    date = date ? date : new Date().getTime()
    typeof date === 'string' ? date =  date.replace(/-/g,'/') : '';
    date = typeof date === "object" ? date : new Date(date);
    let year = date.getFullYear(),
        month = date.getMonth() + 1,
        day = date.getDate(),

        hour = date.getHours(),
        minute = date.getMinutes(),
        second = date.getSeconds(),

        t0 = date.getTime(),
        t1 = [year, month, day].map(formatNumber).join("-"),
        t2 = [year, month, day].map(formatNumber).join("/"),
        t3 = [hour, minute, second].map(formatNumber).join(":"),

        newTime = "";
    switch (type) {
        case "yyyy-MM-dd HH:mm:ss":
            newTime = `${t1} ${t3}`;
            break;
        case "yyyy/MM/dd HH:mm:ss":
            newTime = `${t2} ${t3}`;
            break;
        case "yyyy-MM-dd":
            newTime = t1;
            break;
        case "yyyy":
            newTime = year;
            break;
        case "MM":
            newTime = month;
            break;
        case "dd":
            newTime = day;
            break;
        case "HH":
            newTime = hour;
            break;
        case "mm":
            newTime = minute;
            break;
        case "ss":
            newTime = second;
            break;
        case "yyyy-MM":
            newTime = `${year}-${month}`;
            break;
        case "yyyy/MM/dd":
            newTime = t2;
            break;
        case "timeStamp-13":
            newTime = t0;
            break;
        case "timeStamp-10":
            newTime = String(t0).substr(0,10);
            break;
        default:
            newTime = `${t1} ${t3}`;
            break;
    }
    return newTime;
};
/**
 * 获取星期
 */
function getWeek(time) {
    let arys1 = new Array();
    arys1 = time.split('-');     //日期为输入日期，格式为 2013-3-10
    let ssdate = new Date(arys1[0], parseInt(arys1[1] - 1), arys1[2]);
    let  week1=String(ssdate.getDay()).replace("0","日").replace("1","一").replace("2","二").replace("3","三").replace("4","四").replace("5","五").replace("6","六")//就是你要的星期几
    let week="周"+week1; //就是你要的星期几
    return week;
}

function GetPercent(num, total) {
    /// 求百分比
    num = parseFloat(num);
    total = parseFloat(total);
    if (isNaN(num) || isNaN(total)) {
        return "-";
    }
    return total <= 0 ? "0%" : (Math.round(num / total * 10000) / 100.00)+"%";
}
module.exports = {
    isEmpty,//判断数组是否为空  参数数组 返回 true/false
    GetPercent,//计算百分比  参数（当前值，总值）
    verifyPhone,//验证手机号
    getTime,//
    getQueryString,clear,getWeek,copyObject,hidePhone,hideIdCard
}
